package cn.edu.zut.controller;

import cn.edu.zut.domin.Book;
import cn.edu.zut.service.IBookService;
import com.github.pagehelper.PageInfo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/book")

public class BookController {

    //加载配置文件
    private ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    //获取对象
    private IBookService bookService =(IBookService)ac.getBean("bookServiceImpl");

    @RequestMapping("/findAll")
    //默认第一页,每页显示四个

    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Book> books = bookService.findAll(page, size);
        //分页Bean PageInfo就是一个分页的Bean
        PageInfo<Book> pageInfo = new PageInfo<>(books);
        //总页数等....

        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("Book-list");
        return mv;
    }

    @RequestMapping("/save")
    public String saveBook(Book book) throws Exception {
        bookService.saveBook(book);
        return "redirect:findAll";
    }



    @RequestMapping("/delete")
    public String deleteBookById(String bookId) throws Exception {
        bookService.deleteBook(bookId);
        //删除完成后回到全部信息页面
        return "redirect:findAll";
    }


    @RequestMapping("/update")
    public String updateBook(Book book) throws Exception {
        bookService.updateBook(book);
        return "redirect:findAll";
    }


    @RequestMapping("/findBookByName")
    public ModelAndView findByBookName(String name) throws Exception {
        System.out.println(name);
        ModelAndView mv = new ModelAndView();
        List<Book> users = bookService.findBookByName(name);
        mv.addObject("bookList", users);
        mv.setViewName("Book-list");
        return mv;
    }

    /**
     * 模糊查询
     */
    @RequestMapping("/findBookByNameMOHU")
    public ModelAndView findByBookNameMOHU(String name) throws  Exception{
        System.out.println(name);
        ModelAndView mv = new ModelAndView();
        List<Book> books = bookService.findBookByNameMOHU(name);
        mv.addObject("bookList", books);
        mv.setViewName("Book-list");
        return mv;
    }

    @RequestMapping("/findBookById")
    public ModelAndView findByBookId(String name) throws Exception {
        System.out.println(name);
        ModelAndView mv = new ModelAndView();
        List<Book> users = bookService.findBookById(name);
        mv.addObject("bookList", users);
        mv.setViewName("Book-list");
        return mv;
    }

    @RequestMapping("/findBookByPublisher")
    public ModelAndView findBookByPublisher(String name) throws Exception {
        System.out.println(name);
        ModelAndView mv = new ModelAndView();
        List<Book> books = bookService.findBookByPublisher(name);
        mv.addObject("bookList", books);
        mv.setViewName("Book-list");
        return mv;
    }


}
